package com.ymatou.apigateway.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqProductBaseUpdateBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqProductBaseUpdateCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.godfreightmgr.parameter.CreateCollectionGoodsAddressBean;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.model.godfreight.CollectiongoodsAddress;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品基础信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SqProductBaseUpdate{
	private static OpenapiReq openapiReq;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	private static SqProductBaseUpdateBean sqproductbaseupdateBean;
	private static SqProductBaseUpdateCall sqproductbaseupdateCall;
	private static int RootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品基础信息");
	}

	@Before
	public void caseUp() {
		sqproductbaseupdateBean=new SqProductBaseUpdateBean();
		sqproductbaseupdateCall=new SqProductBaseUpdateCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.product.base.update");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqproductbaseupdateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_SqProductBaseUpdate_001() {
		Logger.start(true,"更新商品基础信息");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
            sqproductbaseupdateBean.setBrand_id(AddAgentProductCallService.BrandId);
            sqproductbaseupdateBean.setCard_info(0);
            sqproductbaseupdateBean.setCounter_currency("JPN");
            sqproductbaseupdateBean.setDeliver_type(1);
            sqproductbaseupdateBean.setProduct_code("货号538257293");
//            sqproductbaseupdateBean.setFreight_template_name("ftname");
            sqproductbaseupdateBean.setProduct_id(productId);
            sqproductbaseupdateBean.setRemark("rmk");
            sqproductbaseupdateBean.setSale_type(1);
            //税率不正确，请从列表中选择 税率在disconf配置 选择项
            sqproductbaseupdateBean.setTariff_rate(9.1d);
            sqproductbaseupdateBean.setThird_category_id(AddAgentProductCallService.CategoryId3_2);
            sqproductbaseupdateBean.setTitle("nt"+TestDataManager.getRandomIntNum(5));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductbaseupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.base.update",
							app_id);
			sqproductbaseupdateCall.setUrl(url);
			sqproductbaseupdateCall.setData(openapiReq);
			sqproductbaseupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductbaseupdateCall.getString("code"), "验证返回");
			Map<String, Object> m = ipm.selectProductByProdId(productId);
			Logger.debug(m);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_SqProductBaseUpdate_002() {
		Logger.start(true,"更新商品基础信息-检查仓库分仓");
		try {
			//创建分仓
//			createDepotBean bean = GodmchManageCallService.createDepot(RootUserId);
//            List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService
//                            .SelectDepotByDepotTitle(bean.getDepotTitle());
			List<mch_depot_info> depotList = GodmchManageCallService
                    .SelectDepotByMchId(RootUserId);
    
			String depotname=depotList.get(0).getDepotTitle();
			Integer depotid=depotList.get(0).getDepotId();
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            sqproductbaseupdateBean.setDepot_Name(depotname);
            sqproductbaseupdateBean.setBrand_id(AddAgentProductCallService.BrandId);
            sqproductbaseupdateBean.setCard_info(0);
            sqproductbaseupdateBean.setCounter_currency("JPN");
            sqproductbaseupdateBean.setDeliver_type(1);
//            sqproductbaseupdateBean.setFreight_template_name("ftname");
            sqproductbaseupdateBean.setProduct_id(productId);
            sqproductbaseupdateBean.setRemark("rmk");
            sqproductbaseupdateBean.setSale_type(1);
            //税率不正确，请从列表中选择 税率在disconf配置 选择项
            sqproductbaseupdateBean.setTariff_rate(9.1d);
            sqproductbaseupdateBean.setThird_category_id(AddAgentProductCallService.CategoryId3_2);
            sqproductbaseupdateBean.setTitle("newTitle");
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductbaseupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.base.update",
							app_id);
			sqproductbaseupdateCall.setUrl(url);
			sqproductbaseupdateCall.setData(openapiReq);
			sqproductbaseupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductbaseupdateCall.getString("code"), "验证返回");
			Map<String, Object> m = ipm.selectProductByProdId(productId);
			Logger.debug(depotid+":"+m.toString());
			Logger.verifyEquals(new BigDecimal(depotid).setScale(0), new BigDecimal( m.get("iBondedArea").toString()).setScale(0), "验证分仓");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_SqProductBaseUpdate_003() {
		Logger.start(true,"更新商品基础信息-不输入可选项不变");
		try {
			List<mch_depot_info> depotList = GodmchManageCallService
                    .SelectDepotByMchId(RootUserId);
    
			String depotname=depotList.get(0).getDepotTitle();
			Integer depotid=depotList.get(0).getDepotId();
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
			
//			brand_id	Integer	否		品牌id
			addAgentProductBean.setCatalogStatus(1);
			addAgentProductBean.setBrandId(AddAgentProductCallService.BrandId);
//			deliver_type	Integer	否		物流方式 100：海外物流，999：其他，4：保税仓发货
//			card_info	Integer	否		买家身份信息 0：不需要，1：提供身份证号码，2：提供身份证图片
			addAgentProductBean.setCardInfo(1);
			addAgentProductBean.setCatalogStatus(100);
//			currency	String	否		商品定价币种 默认：CNY
			addAgentProductBean.setCurrency("USD");
//			remark	String	否		备注 仅自己和客服可见
			addAgentProductBean.setRemark("remark1");
//			tariff_rate	Double	否		税率百分比 可选值： 0, 9.1, 23.059
			addAgentProductBean.setTariffRate(0.091);
//			counter_currency	String	否		专柜价币种
//			freight_template_name	String	否		运费模板名称 不填则包邮
//			sale_type	int	是	1:上架，2下架	上架类型
			addAgentProductBean.setSaleType(1);
//			depot_name	String	否		仓库名
			addAgentProductBean.setDepotId(depotid);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            Map<String, Object> m = ipm.selectProductByProdId(productId);
			Logger.debug("product:"+m.toString());
			Map<String, Object> magent = ipm.selectAgentProductsByProdId(productId);
			Logger.debug("agproduct:"+magent.toString());
//            sqproductbaseupdateBean.setDepot_Name(depotname);
//            sqproductbaseupdateBean.setBrand_id(AddAgentProductCallService.BrandId);
//            sqproductbaseupdateBean.setCard_info(0);
//            sqproductbaseupdateBean.setCounter_currency("JPN");
//            sqproductbaseupdateBean.setDeliver_type(1);
//            sqproductbaseupdateBean.setFreight_template_name("ftname");
            sqproductbaseupdateBean.setProduct_id(productId);
//            sqproductbaseupdateBean.setRemark("rmk");
            sqproductbaseupdateBean.setSale_type(1);
            //税率不正确，请从列表中选择 税率在disconf配置 选择项
//            sqproductbaseupdateBean.setTariff_rate(9.1d);
//            sqproductbaseupdateBean.setThird_category_id(AddAgentProductCallService.CategoryId3_2);
            sqproductbaseupdateBean.setTitle("newTitle");
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductbaseupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.base.update",
							app_id);
			sqproductbaseupdateCall.setUrl(url);
			sqproductbaseupdateCall.setData(openapiReq);
			sqproductbaseupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductbaseupdateCall.getString("code"), "验证返回");
			 m = ipm.selectProductByProdId(productId);
			Logger.debug("product2:"+m.toString());
			Logger.verifyEquals(addAgentProductBean.getBrandId().toString(), m.get("iBrandId").toString(), "验证iBrandId");
			Logger.verifyEquals(addAgentProductBean.getThirdCategoryId().toString(), m.get("iThirdCategoryId").toString(), "验证iThirdCategoryId");
			Logger.verifyEquals(addAgentProductBean.getCatalogStatus().toString(), m.get("iCatalogStatus").toString(), "验证iCatalogStatus");
			Logger.verifyEquals(addAgentProductBean.getCardInfo().toString(), m.get("CardInfo").toString(), "验证cardInfo");
			Logger.verifyEquals(new BigDecimal(depotid).setScale(0), new BigDecimal( m.get("iBondedArea").toString()).setScale(0), "验证分仓");
			 magent = ipm.selectAgentProductsByProdId(productId);
			Logger.debug("agproduct2:"+magent.toString());
			Logger.verifyEquals( new BigDecimal( addAgentProductBean.getTariffRate()).setScale(5,BigDecimal.ROUND_HALF_UP),  new BigDecimal( magent.get("TariffRate").toString()).setScale(5,BigDecimal.ROUND_HALF_UP), "验证TariffRate");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_SqProductBaseUpdate_004() {
		Logger.start(true,"更新商品基础信息-Product_param");
		try {
			//前置数据
			//用户未开通商品参数信息功能-需要mchtype=2
			Integer userid=500811132;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userid,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            sqproductbaseupdateBean.setProduct_param("{\"类目\":\"运动鞋\"}");
            sqproductbaseupdateBean.setBrand_id(AddAgentProductCallService.BrandId);
            sqproductbaseupdateBean.setCard_info(0);
            sqproductbaseupdateBean.setCounter_currency("JPN");
            sqproductbaseupdateBean.setDeliver_type(1);
//            sqproductbaseupdateBean.setFreight_template_name("ftname");
            sqproductbaseupdateBean.setProduct_id(productId);
            sqproductbaseupdateBean.setRemark("rmk");
            sqproductbaseupdateBean.setSale_type(1);
            //税率不正确，请从列表中选择 税率在disconf配置 选择项
            sqproductbaseupdateBean.setTariff_rate(9.1d);
            sqproductbaseupdateBean.setThird_category_id(AddAgentProductCallService.CategoryId3_2);
            sqproductbaseupdateBean.setTitle("nt"+TestDataManager.getRandomIntNum(5));
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) userid).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductbaseupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.base.update",
							app_id);
			sqproductbaseupdateCall.setUrl(url);
			sqproductbaseupdateCall.setData(openapiReq);
			sqproductbaseupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductbaseupdateCall.getString("code"), "验证返回");
			Map<String, Object> pmap = ipm.selectAgentProductsByProdId(productId);
			Logger.debug(pmap.toString());
			Logger.verifyEquals(sqproductbaseupdateBean.getProduct_param(),pmap.get("ProductParam").toString(), "验证ProductParam");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_SqProductBaseUpdate_005() {
		Logger.start(true,"更新商品基础信息-副标题");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
             CollectiongoodsAddress an = GodfreightmgrService.getCollectionGoodsAddresslist(RootUserId).get(0);
            sqproductbaseupdateBean.setTitle(addAgentProductBean.getTitle());
            sqproductbaseupdateBean.setSale_type(1);
            sqproductbaseupdateBean.setProduct_id(productId);
            sqproductbaseupdateBean.setSubtitle("subtitle1");
            sqproductbaseupdateBean.setSale_area(1);
            sqproductbaseupdateBean.setCollection_goods_address(Arrays.asList(an.getAddressName()));
            sqproductbaseupdateBean.setNoreason_return(true);
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductbaseupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.base.update",
							app_id);
			sqproductbaseupdateCall.setUrl(url);
			sqproductbaseupdateCall.setData(openapiReq);
			sqproductbaseupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductbaseupdateCall.getString("code"), "验证返回");
			Map<String, Object> m = ipm.selectProductByProdId(productId);
			Logger.verifyEquals(m.get("bNoReasonReturn"), sqproductbaseupdateBean.getNoreason_return(), "bNoReasonReturn");
			Map<String, Object> am = ipm.selectAgentProductsByProdId(productId);			
			Logger.debug(FormatUtil.beanToGSONString(am));
			Logger.verifyEquals(am.get("SubTitle"), sqproductbaseupdateBean.getSubtitle(), "验证Subtitle");
			Logger.verifyEquals(am.get("SaleArea"), sqproductbaseupdateBean.getSale_area(), "验证SaleArea");
			Logger.verifyIsContains(an.getAddressId(), am.get("CollectionGoodsAddress"), "验证CollectionGoodsAddress");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}